package com.sodaopen.crm.sys.service.impl;

import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sodaopen.crm.sys.entity.User;
import com.sodaopen.crm.sys.mapper.UserMapper;
import com.sodaopen.crm.sys.service.IUserService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SodaJava
 * @since 2020-07-02
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

	@Override
    public User findByUsername(String username) {
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("username", username);
        return this.getOne(query);
    }

    @Override
    public Page<User> queryUsers(Map<String, Object> params, Integer page, Integer limit) {
        LambdaQueryWrapper<User> query = new LambdaQueryWrapper<>();
        query.like(!StringUtils.isEmpty(params.get("username")), User::getUsername, params.get("username"));
        query.like(!StringUtils.isEmpty(params.get("name")), User::getName, params.get("name"));
        query.like(!StringUtils.isEmpty(params.get("email")), User::getEmail, params.get("email"));
        query.like(!StringUtils.isEmpty(params.get("mobile")), User::getMobile, params.get("mobile"));

        return page(new Page<>(page, limit), query);
    }
}
